﻿using System;
using System.IO;
using fastCSharp;

namespace diantou.examImageServer
{
    /// <summary>
    /// 考试用户图片服务
    /// </summary>
    [fastCSharp.code.cSharp.tcpServer(Service = "ExamImage", IsIdentityCommand = true, VerifyMethodType = typeof(server.tcpClient.timeVerifyMethod))]
    public partial class server : fastCSharp.net.tcp.timeVerifyServer
    {
        /// <summary>
        /// 图片服务配置
        /// </summary>
        internal sealed class config
        {
            /// <summary>
            /// 图片路径
            /// </summary>
            public string Path;
        }
        /// <summary>
        /// 图片服务配置
        /// </summary>
        internal static readonly config Config = fastCSharp.config.pub.LoadConfig(new config());
        /// <summary>
        /// 图片路径
        /// </summary>
        public static string Path
        {
            get { return Config.Path; }
        }

        /// <summary>
        /// 获取文件名称集合
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        [fastCSharp.code.cSharp.tcpMethod]
        private int[] getNames(int userId)
        {
            string path = Config.Path + userId.toString() + @"\";
            return Directory.Exists(path) ? Directory.GetFiles(path).getArray(name => int.Parse(name.Substring(name.LastIndexOf('\\') + 1))).sort() : null;
        }
        /// <summary>
        /// 批量获取文件内容
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="names"></param>
        /// <returns></returns>
        [fastCSharp.code.cSharp.tcpMethod]
        private byte[][] get(int userId, subArray<int> names)
        {
            string path = Config.Path + userId.toString() + @"\";
            byte[][] datas = new byte[names.Count][];
            int index = 0;
            foreach (int name in names) datas[index++] = File.ReadAllBytes(path + name.toString());
            return datas;
        }
        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="userId"></param>
        [fastCSharp.code.cSharp.tcpMethod]
        private void remove(int userId)
        {
            DirectoryInfo path = new DirectoryInfo(Config.Path + userId.toString() + @"\");
            if (path.Exists)
            {
                foreach (FileInfo file in path.GetFiles()) file.Delete();
                path.Delete();
            }
        }
    }
}
